﻿Imports System.Net
Imports System.Net.NetworkInformation
Public Class netInfo

    Private Sub netInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Public Sub getNetInfo()

        Dim LocalHostName As String
        LocalHostName = System.Net.Dns.GetHostName
        lblLocalHost.Text = LocalHostName
        '  Dim sysInfo As System.Net.NetworkInformation.IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties
        '  Dim ipStat As IPGlobalStatistics = Nothing
        ' ipStat = sysInfo.GetIPv4GlobalStatistics
        ' ipStat.
        Dim adapters As System.Net.NetworkInformation.NetworkInterface() = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As System.Net.NetworkInformation.NetworkInterface

        For Each adapter In adapters
            lbAdapters.Items.Add(adapter.Name)
            'ipProp = adapter.GetIPv4Statistic
            'ipProp.GetIPv()
        Next adapter
        If lbAdapters.Items.Count > 0 Then
            lbAdapters.SelectedIndex = 0
        End If
        getIpAddresses()

        pb1.Visible = True
        lblScanning.Visible = True
        Dim thead As New Threading.Thread(AddressOf netStat)
        thead.Start(Form1.lblLoalIp.Text)


    End Sub

    Private Sub getAdapterInfo(ByVal adapter As System.Net.NetworkInformation.NetworkInterface)
        lblStatus.Text = adapter.OperationalStatus.ToString
        lblMac.Text = adapter.GetPhysicalAddress.ToString
        lblDecription.Text = adapter.Description
        lblType.Text = adapter.NetworkInterfaceType.ToString


    End Sub

    Private Sub lbAdapters_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbAdapters.SelectedIndexChanged
        If Not lbAdapters.SelectedItem Is Nothing Then
            Dim adpname As String = lbAdapters.SelectedItem.ToString
            Dim adapters As System.Net.NetworkInformation.NetworkInterface() = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
            Dim adapter As System.Net.NetworkInformation.NetworkInterface

            For Each adapter In adapters
                If adapter.Name = adpname Then
                    getAdapterInfo(adapter)
                End If
                'ipProp = adapter.GetIPv4Statistic
                'ipProp.GetIPv()
            Next adapter
        End If
    End Sub
    Private Sub getIpAddresses()
        Dim LocalHostName As String

        LocalHostName = System.Net.Dns.GetHostName()
        Dim ipEnter As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(LocalHostName)
        For Each curAdd In ipEnter.AddressList
            ' Display the type of address family supported by the server. If the
            ' server is IPv6-enabled this value is: InternNetworkV6. If the server
            ' is also IPv4-enabled there will be an additional value of InterNetwork.
            ' Console.WriteLine(("AddressFamily: " + curAdd.AddressFamily.ToString()))
            Dim str As String = curAdd.AddressFamily.ToString & " : " & curAdd.ToString
            lbAddress.Items.Add(str)

            ' Display the server IP address in the standard format. In 
            ' IPv4 the format will be dotted-quad notation, in IPv6 it will be
            ' in in colon-hexadecimal notation.
            ' Console.WriteLine(("Address: " + curAdd.ToString()))

            ' Display the server IP address in byte format.
            'Console.Write("AddressBytes: ")
        Next curAdd
    End Sub

    Private Delegate Sub UpdateUIDelegate(ByVal data As String)
    Private Delegate Sub UpdatePBDelegate()
    Private Delegate Sub UpdateHideDelegate()
    Private Sub netStat(ByVal localIp)
        Dim pinger As New Ping
        Dim ip As String = localIp



        If ip <> "" Then
            Dim ips() As String = ip.Split(".")
            'Dim x1 As Integer = 1
            Dim y1 As Integer = 1

            'For x1 = 1 To 1

            For y1 = 1 To 255
                Dim tempIP As String = ips(0) & "." & ips(1) & "." & ips(2) & "." & y1
                Dim results As PingReply = Nothing
                UpdatePb()
                Try
                    results = pinger.Send(tempIP, 100)
                Catch ex As Exception

                End Try
                If Not results Is Nothing Then
                    If results.Status = IPStatus.Success Then
                        Dim LocalHostName As String
                        LocalHostName = System.Net.Dns.GetHostEntry(tempIP).HostName
                        LocalHostName = LocalHostName & " : " & tempIP
                        UpdateUI(LocalHostName)

                    End If
                End If


            Next
            'Next
            UpdateHide()


        End If




    End Sub
    Private Sub UpdateUI(ByVal str As String)
        If Me.InvokeRequired Then

            Me.Invoke(New UpdateUIDelegate(AddressOf delAddItem), str)
        Else
            delAddItem(str)
        End If
    End Sub
    Private Sub UpdatePb()
        If Me.InvokeRequired Then

            Me.Invoke(New UpdatePBDelegate(AddressOf updateUpSub))
        Else
            updateUpSub()
        End If
    End Sub
    Private Sub UpdateHide()
        If Me.InvokeRequired Then

            Me.Invoke(New UpdateHideDelegate(AddressOf hideItems))
        Else
            hideItems()
        End If
    End Sub
    Private Sub hideItems()
        pb1.Visible = False
        lblScanning.Visible = False
    End Sub
    Private Sub updateUpSub()
        pb1.Value = pb1.Value + 1
    End Sub
    Private Sub delAddItem(ByVal str As String)
        lbComputers.Items.Add(str)
    End Sub
End Class